package com.nfcuttings.presuvinil.database;

import java.util.ArrayList;
import java.util.List;

import android.app.Activity;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase;

import com.nfcuttings.presuvinil.Material;

public class MaterialDataSource extends Activity{

	private static MaterialDataSource dataSource;

//	public static ArrayList<Material> getMateriales() {
//		ArrayList<Material> materiales = new ArrayList<Material>();
//		materiales.add(new Material("Oracal 651", 61, 98.2));
//		materiales.add(new Material("Termotransferible", 50, 377));
//		materiales.add(new Material("Termotransferible NEON", 50, 420));
//
//		return materiales;//TODO sacar de la base de datos
//	}

	// Database fields
	private SQLiteDatabase database;
	private MaterialSQLiteHelper dbHelper;
	private String[] allColumns = { MaterialSQLiteHelper.COLUMN_ID,
			MaterialSQLiteHelper.COLUMN_DESCRIPTION, MaterialSQLiteHelper.COLUMN_COSTOMETRO,
			MaterialSQLiteHelper.COLUMN_ANCHO};

	public static MaterialDataSource getDataSource(Context context) {
		if(dataSource == null){
			dataSource = new MaterialDataSource();
			dataSource.dbHelper = new MaterialSQLiteHelper(context);
			return dataSource;
		}
		return dataSource;
	}


	public void open() throws SQLException {
		database = dbHelper.getWritableDatabase();
	}

	public void close() {
		dbHelper.close();
	}

	public Material createMaterial(String descripcion, double costoxmetro, double ancho) {
		ContentValues values = new ContentValues();
		values.put(MaterialSQLiteHelper.COLUMN_DESCRIPTION, descripcion);
		values.put(MaterialSQLiteHelper.COLUMN_COSTOMETRO, costoxmetro);
		values.put(MaterialSQLiteHelper.COLUMN_ANCHO, ancho);
		long insertId = database.insert(MaterialSQLiteHelper.TABLE_MATERIAL, null,
				values);
		Cursor cursor = database.query(MaterialSQLiteHelper.TABLE_MATERIAL,
				allColumns, MaterialSQLiteHelper.COLUMN_ID + " = " + insertId, null,
				null, null, null);
		cursor.moveToFirst();
		Material newMaterial = cursorToMaterial(cursor);
		cursor.close();
		return newMaterial;
	}

	public void deleteMaterial(Material material) {
		long id = material.getId();
		System.out.println("Material deleted with id: " + id);
		database.delete(MaterialSQLiteHelper.TABLE_MATERIAL, MaterialSQLiteHelper.COLUMN_ID
				+ " = " + id, null);
	}

	public List<Material> getAllMaterials() {
		List<Material> Materials = new ArrayList<Material>();

		Cursor cursor = database.query(MaterialSQLiteHelper.TABLE_MATERIAL,
				allColumns, null, null, null, null, null);

		cursor.moveToFirst();
		while (!cursor.isAfterLast()) {
			Material Material = cursorToMaterial(cursor);
			Materials.add(Material);
			cursor.moveToNext();
		}
		// Make sure to close the cursor
		cursor.close();
		return Materials;
	}

	private Material cursorToMaterial(Cursor cursor) {
		Material Material = new Material();
		Material.setId(cursor.getLong(0));
		Material.setDescripcion(cursor.getString(1));
		Material.setCostoxmetro(cursor.getDouble(2));
		Material.setAncho(cursor.getDouble(3));
		return Material;
	}



}
